"""
内容：放置公共方法和类
作者：7
"""
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from ECShop_no9.common.pymysql_09 import pymysql_09
from selenium.webdriver.common.keys import Keys
import os

# 创建浏览器函数
def create_browser():
    my = webdriver.Chrome()
    return my
# 创建浏览器配置对象
def create_browser_op(path):
    op = webdriver.ChromeOptions()
    user_data_dir = fr"--user-data-dir={path}"
    op.add_argument(user_data_dir)
    # 创建浏览器
    my = webdriver.Chrome(options=op)
    return my
"""无头模式加配置项"""
def create_browser_headless_op(path):
    op = webdriver.ChromeOptions()
    op.add_argument("-headless")
    user_data_dir = fr"--user-data-dir={path}"
    op.add_argument(user_data_dir)
    my = webdriver.Chrome(options=op)
    return my
"""无头模式"""
def create_browser_headless(path):
    op = webdriver.ChromeOptions()
    op.add_argument("-headless")
    my = webdriver.Chrome(options=op)
    return my
"""数据库判定添加成功"""
def base_sql_add(sql_content,key):
    # 定义属性
    def path():
        data_path = os.path.dirname(os.path.dirname(__file__))+ f'./common/data.ini'
        return data_path
    new_sql = pymysql_09(path())
    sql = sql_content
    return new_sql.show_data(sql)[-1][key]
"""数据库判定删除成功"""
def base_sql_del(sql_content,key):
    # 定义属性
    def path():
        data_path = os.path.dirname(os.path.dirname(__file__)) + f'/data/data.ini'
        return data_path
    new_sql = pymysql_09(path())
    sql = sql_content
    return new_sql.show_data(sql)[-1][key]

# 创建一个Base类
class Base(object):
    # 定义属性
    def __init__(self, my):
        self.my = my



    # 定义方法
    """确定网页"""

    def base_open_web(self, url):
        self.my.get(url)

    """输入信息"""

    def base_input_content(self, your_locator, your_content, time=10):
        WebDriverWait(self.my, time).until(EC.presence_of_element_located(your_locator)).send_keys(your_content)

    """点击"""

    def base_click(self, your_locator, time=10):
        WebDriverWait(self.my, time).until(EC.presence_of_element_located(your_locator)).click()

    """获取文本"""

    def base_get_text(self, your_locator, time=10):
        try:
            result = WebDriverWait(self.my, time).until(EC.presence_of_element_located(your_locator)).text
        except:
            return None
        else:
            return result
        finally:
            pass

    """滚动条到自定义位置"""

    def base_roll1(self, h, l):
        js = f"window.scrollTo({h},{l})"
        self.my.execute_script(js)

    """多窗口切换"""

    def base_window(self, n):
        self.my.switch_to.window(self.my.window_handles[n])
    """获取文本复杂写法"""
    def base_get_text1(self,yours):
        try:
            result= self.my.find_element_by_css_selector(yours).text
        except :
            return None
        else:
            return result
        finally:
            pass
    """进入frame"""
    def base_in_frame(self,num):
        self.my.switch_to.frame(num)
    """退出frame"""
    def base_quit_frame(self):
        self.my.switch_to.default_content()
    """下拉框"""
    def base_select(self,your_locator,your_content,time=10):
        Select(WebDriverWait(self.my,time).until(EC.presence_of_element_located(your_locator))).select_by_visible_text(your_content)
    """滚动条操作"""
    def base_roll(self,your_locator,time=10):
        js = 'arguments[0].scrollIntoView()'
        element = WebDriverWait(self.my,time).until(EC.presence_of_element_located(your_locator))
        self.my.execute_script(js,element)
    """弹窗操作-->确定"""
    def base_alert(self):
        self.my.switch_to.alert.accept()

    """弹窗操作-->取消"""
    def base_alert_dismiss(self):
        self.my.switch_to.alert.dismiss()
    """获取源代码不简写"""

    def base_get_attribute(self, yours):
        return self.my.find_element_by_css_selector(yours).get_attribute('outerHTML')
    """获取对应属性的值"""
    def base_get_value(self,your_selector,your_element_name,time = 10):
        return WebDriverWait(self.my,time).until(EC.presence_of_element_located(your_selector)).get_attribute(your_element_name)
    """弹窗获取文本"""
    def base_alert_text(self):
        alter_text=self.my.switch_to.alert
        return alter_text.text
    """最大化窗口"""
    def base_window_max(self):
        self.my.maximize_window()
    """退格"""
    def base_backspace(self,your_locator,time=10):
        WebDriverWait(self.my,time).until(EC.presence_of_element_located(your_locator)).send_keys(Keys.BACK_SPACE)
    """判断是否可用"""
    def base_enabled(self,your_locator,time=10):
        return WebDriverWait(self.my,time).until(EC.presence_of_element_located(your_locator)).is_enabled()
    """连接数据库断言----->判断是否添加成功"""
    def base_con_sql_insert(self,ini_path,sql_content):
        sql = pymysql_09(ini_path)
        show=sql_content
        sql.show_data(show)
    """输入框清空"""
    def base_input_clear(self,your_locator,time=10):
        WebDriverWait(self.my, time).until(EC.presence_of_element_located(your_locator)).clear()
    """执行js"""
    def base_execute_js(self,your_js):
        self.my.execute_script(your_js)
    """关闭"""
    def base_quit(self):
        self.my.quit()

    